package com.jabo.douban.demo.domain.http;


import com.jabo.douban.demo.model.entity.BaseBean;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * @anthor JZheng3
 * @Time: 2020/3/3
 */
public class RxNet {
    public static <T> Observable<BaseBean<T>> requestWrapper(Observable<BaseBean<T>> observable) {
        return observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .onErrorReturn(new Function<Throwable, BaseBean<T>>() {
                    @Override
                    public BaseBean<T> apply(Throwable throwable) throws Exception {
                        BaseBean<T> tBaseBean = new BaseBean<>();
                        tBaseBean.setServerCode(-1);
                        tBaseBean.setServerMessage(ExceptionHandle.handleException(throwable));
                        return tBaseBean;
                    }
                });
    }

//    public static Observable<? extends BaseBean> requestWrappers(Observable<? extends BaseBean> observable) {
//        return observable.subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .onErrorReturn(new U);
//    }
}
